package com.info.zhiduoduo.core.configs;

import com.alibaba.fastjson.JSON;
import com.info.zhiduoduo.core.configs.Redis.RedisKey;
import com.info.zhiduoduo.common.utils.LogUtil;
import com.info.zhiduoduo.core.service.common.RedisService;
import com.info.zhiduoduo.wxsdk.config.IWxAccessTokenCallbackConfig;
import com.info.zhiduoduo.wxsdk.config.WxZddMakerConfig;
import com.info.zhiduoduo.wxsdk.domain.app.WxNormalAccessToken;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan({"com.info.zhiduoduo.core.service"})
@EnableConfigurationProperties(WxZddMakerConfig.class)
public class WxAccessTokenCallbackConfig implements IWxAccessTokenCallbackConfig {

	/*@Override
	public WxNormalAccessToken getLatestAccessToken() {
		WxAccessToken latestWxAccessToken = new WxAccessToken()
				.selectOne(new EntityWrapper()
						.orderBy("id", false)
						.last("limit 1"));
		if (latestWxAccessToken == null) {
			return null;
		}
		WxNormalAccessToken token = new WxNormalAccessToken();
		token.setAccess_token(latestWxAccessToken.getAccessToken());
		token.setCreatetime(latestWxAccessToken.getCreatetime());
		token.setExpires_in(latestWxAccessToken.getExpiresIn());
		token.setJsticket(latestWxAccessToken.getJsticket());
		return token;
	}

	@Override
	public void saveNewNormalAccessToken(WxNormalAccessToken token) {
		WxAccessToken weixinAccessToken = new WxAccessToken();
		weixinAccessToken.setAccessToken(token.getAccess_token());
		weixinAccessToken.setExpiresIn(token.getExpires_in());
		weixinAccessToken.setJsticket(token.getJsticket());
		weixinAccessToken.setCreatetime(token.getCreatetime());
		boolean insert = weixinAccessToken.insert();
		LogUtil.info("saveNewNormalAccessToken : " +insert + " ,token : "+ token.toString());
	}*/

	@Autowired
	RedisService redisService;

	@Autowired
	WxZddMakerConfig wxZddMakerConfig;

	@Override
	public WxNormalAccessToken getLatestAccessToken() {
		String value = redisService.getString(RedisKey.genWxAccessTokenKey(wxZddMakerConfig.getAppid()));
		WxNormalAccessToken token = JSON.parseObject(value, WxNormalAccessToken.class);
		LogUtil.info("redis 获取到的 token = " + token);
		return token;
	}

	@Override
	public WxNormalAccessToken getLatestAccessToken(String appid) {
		String value = redisService.getString(RedisKey.genWxAccessTokenKey(appid));
		WxNormalAccessToken token = JSON.parseObject(value, WxNormalAccessToken.class);
		LogUtil.info("redis 获取到的 token = " + token);
		return token;
	}

	@Override
	public void saveNewNormalAccessToken(WxNormalAccessToken token) {
		boolean insert = redisService.set(RedisKey.genWxAccessTokenKey(wxZddMakerConfig.getAppid()), JSON.toJSONString(token));
		LogUtil.info("saveNewNormalAccessToken : " + insert + " ,token : " + token.toString());
	}

	@Override
	public void saveNewNormalAccessToken(WxNormalAccessToken token, String appid) {
		boolean insert = redisService.set(RedisKey.genWxAccessTokenKey(appid), JSON.toJSONString(token));
		LogUtil.info("saveNewNormalAccessToken : " + insert + " ,token : " + token.toString());
	}
}
